<template lang="html">
  <div class="header">
    <div class="header-left">
      <img src="~common/images/mylogo.png" alt="" width="160">
    </div>
    <div class="header-right">
      <div class="avatar-container">
        <img :src="userInfo.avatar" alt="" width="40" height="40" class="avatar">
      </div>
      <div class="right-container">
        <el-dropdown trigger="click" :style="{padding:'0 10px'}" class="header-drop" @command="handleCommand" >
          <span class="el-dropdown-link" :style="{color:'#fff',fontSize:'16px'}">
            {{userInfo.name}}<i class="el-icon-caret-bottom el-icon--right" :style="{fontSize:'12px'}"></i>
          </span>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item command="modify">修改密码</el-dropdown-item>
            <el-dropdown-item command="exit">退出</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>

      </div>
    </div>
    <el-dialog title="修改密码" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="原始密码">
          <el-input v-model="form.originalPassword" auto-complete="off" type="password" ref="aaaa"></el-input>
        </el-form-item>
        <el-form-item label="新密码">
          <el-input v-model="form.newPassword" auto-complete="off" type="password"></el-input>
        </el-form-item>
        <el-form-item label="确认密码">
          <el-input v-model="form.confirmPassword" auto-complete="off" type="password"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="sure">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  loadToken,
  clear,
  loadKey
} from 'common/js/cache'

import {
  verifyPassWord
} from 'common/js/regexp'

import {
  changePass,
  getUserInfo
} from 'api/header'

import {mapGetters} from 'vuex'

export default {
  data() {
    return {
      userInfo: {},
      dialogTableVisible: false,
      dialogFormVisible: false,
      form: {
        originalPassword: '',
        newPassword: '',
        confirmPassword: ''
      },
      jse: ''
    }
  },
  computed: {
    ...mapGetters([
      'publicKey'
    ])
  },
  methods: {
    goLogin() {
      clear()
      this.$router.push('/backend/login')
    },
    handleCommand(command) {
      switch (command) {
        case 'modify':
          this.dialogFormVisible = true
          this.form.originalPassword = ''
          this.form.newPassword = ''
          this.form.confirmPassword = ''
          break
        case 'exit':
          this.goLogin()
          break
      }
    },
    sure() {
      if (this._checkOriginalPassword() && this._checkNewPassword() && this._checkConfirmPassword()) {
        this.dialogFormVisible = false
        // 密码加密
        this.jse = new this.$jsEncrypt.JSEncrypt()
        this.jse.setPublicKey(loadKey())
        let encryptedOld = this.jse.encrypt(this.form.originalPassword)
        let encryptedNew = this.jse.encrypt(this.form.newPassword)
        let Base64 = require('js-base64').Base64
        Base64.encode(encryptedOld)
        Base64.encode(encryptedNew)
        // 密码加密结束
        changePass(encryptedOld, encryptedNew, loadToken()).then((res) => {
          this.$message({
            showClose: true,
            message: '密码修改成功',
            type: 'success'
          })
        }).catch(() => {
          this.$message({
            showClose: true,
            message: '原始密码不正确',
            type: 'error'
          })
        })
      }
    },
    _checkOriginalPassword() {
      let originalPassword = this.form.originalPassword
      if (originalPassword) {
        if (!verifyPassWord(originalPassword)) {
          this.$message({
            showClose: true,
            message: '原始密码格式不正确',
            type: 'error'
          })
          return false
        } else {
          return true
        }
      } else {
        this.$message({
          showClose: true,
          message: '请输入原始密码',
          type: 'error'
        })
        return false
      }
    },
    _checkNewPassword() {
      let newPassword = this.form.newPassword
      if (newPassword) {
        if (!verifyPassWord(newPassword)) {
          this.$message({
            showClose: true,
            message: '新密码格式不正确',
            type: 'error'
          })
          return false
        } else {
          return true
        }
      } else {
        this.$message({
          showClose: true,
          message: '请输入新密码',
          type: 'error'
        })
        return false
      }
    },
    _checkConfirmPassword() {
      let newPassword = this.form.newPassword
      let confirmPassword = this.form.confirmPassword
      if (!confirmPassword) {
        this.$message({
          showClose: true,
          message: '请输入确认密码',
          type: 'error'
        })
        return false
      } else if (newPassword !== confirmPassword) {
        this.$message({
          showClose: true,
          message: '新密码与确认密码不一致，请检查',
          type: 'error'
        })
        return false
      }
      return true
    }
  },
  created() {
    getUserInfo(loadToken()).then((res) => {
      this.userInfo = res.item
    }).catch((err) => {
      console.log(err)
    })
  }
}
</script>

<style lang="scss">
@import "~common/sass/variable.scss";
@import "~common/sass/ele-reset.scss";
.header {
    width: 100%;
    background: $bg-header;
    height: 50px;
    display: flex;
    justify-content: space-between;
    padding-right: 34px;
    box-sizing: border-box;
    .header-left {
        width: 220px;
        background: $bg-header-left;
        display: flex;
        justify-content: center;
        align-items: center;
    }
    .header-right {
        display: flex;
        cursor: pointer;
        .avatar-container {
            width: 40px;
            height: 40px;
            border-radius: 50%;
            overflow: hidden;
            margin-top: 5px;
            margin-right: 10px;
        }
        .right-container {
            color: #fff!important;
            font-size: 16px;
            line-height: 50px;
            .header-drop:hover {
                background: #204d74;
            }
        }
    }
}
</style>
e>
    }
    }
}
</style>
e>
  }
}
</style>
e>
